home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / bash-1.12 / dist / shell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-21  |  39.0 KB  |  1,616 lines

  1. /* shell.c -- GNU's idea of the POSIX shell specification.
  2.  
  3.    This file is part of Bash, the Bourne Again SHell.  Bash is free
  4.    software; no one can prevent you from reading the source code, or
  5.    giving it to someone else.  This file is copyrighted under the GNU
  6.    General Public License, which can be found in the file called
  7.    COPYING.
  8.  
  9.    Copyright (C) 1988, 1991 Free Software Foundation, Inc.
  10.  
  11.    This file is part of GNU Bash.
  12.  
  13.    Bash is distributed in the hope that it will be useful, but WITHOUT
  14.    ANY WARRANTY.  No author or distributor accepts responsibility to
  15.    anyone for the consequences of using it or for whether it serves
  16.    any particular purpose or works at all, unless he says so in
  17.    writing.  Refer to the GNU Emacs General Public License for full
  18.    details.
  19.  
  20.    Everyone is granted permission to copy, modify and redistribute
  21.    Bash, but only under the conditions described in the GNU General
  22.    Public License.  A copy of this license is supposed to have been
  23.    given to you along with GNU Emacs so you can know your rights and
  24.    responsibilities.  It should be in a file named COPYING.
  25.  
  26.    Among other things, the copyright notice and this notice must be
  27.    preserved on all copies.
  28.  
  29.   Birthdate:
  30.   Sunday, January 10th, 1988.
  31.   Initial author: Brian Fox
  32. */
  33.  
  34. #include <stdio.h>
  35. #include <sys/types.h>
  36. #include <signal.h>
  37. #include <errno.h>
  38. #include <sys/file.h>
  39. #include <pwd.h>
  40. #include "posixstat.h"
  41. #include "filecntl.h"
  42.  
  43. #if defined (HAVE_VFPRINTF)
  44. #include <varargs.h>
  45. #endif
  46.  
  47. #include "shell.h"
  48. #include "flags.h"
  49.  
  50. #if defined (JOB_CONTROL)
  51. #include "jobs.h"
  52. #endif /* JOB_CONTROL */
  53.  
  54. #if defined (USG) && !defined (isc386) && !defined (sgi)
  55. struct passwd *getpwuid ();
  56. #endif
  57.  
  58. extern char *dist_version;
  59. extern int build_version;
  60. extern void using_history ();
  61.  
  62. extern int yydebug;
  63. #if !defined (errno)
  64. extern int errno;
  65. #endif
  66.  
  67. /* Non-zero means that this shell has already been run; i.e. you should
  68.    call shell_reinitialize () if you need to start afresh. */
  69. int shell_initialized = 0;
  70.  
  71. /* The current maintainer of the shell.  You change this in the
  72.    Makefile. */
  73. #if !defined (MAINTAINER)
  74. #define MAINTAINER "deliberately-anonymous"
  75. #endif
  76.  
  77. char *the_current_maintainer = MAINTAINER;
  78.  
  79. #ifndef PPROMPT
  80. #define PPROMPT "bash\\$ "
  81. #endif
  82. char *primary_prompt = PPROMPT;
  83.  
  84. #if !defined (SPROMPT)
  85. #  define SPROMPT "> "
  86. #endif
  87.  
  88. char *secondary_prompt = SPROMPT;
  89.  
  90. COMMAND *global_command = (COMMAND *)NULL;
  91.  
  92. /* Non-zero after SIGINT. */
  93. int interrupt_state = 0;
  94.  
  95. /* The current user's name. */
  96. char *current_user_name = (char *)NULL;
  97.  
  98. /* The current host's name. */
  99. char *current_host_name = (char *)NULL;
  100.  
  101. /* Non-zero means that this shell is a login shell.
  102.    Specifically:
  103.    0 = not login shell.
  104.    1 = login shell from getty (or equivalent fake out)
  105.   -1 = login shell from "-login" flag.
  106.   -2 = both from getty, and from flag.
  107.  */
  108. int login_shell = 0;
  109.  
  110. /* Non-zero means that at this moment, the shell is interactive. */
  111. int interactive = 0;
  112.  
  113. /* Non-zero means that the shell was started as an interactive shell. */
  114. int interactive_shell = 0;
  115.  
  116. /* Non-zero means to remember lines typed to the shell on the history
  117.    list.  This is different than the user-controlled behaviour; this
  118.    becomes zero when we read lines from a file, for example. */
  119. int remember_on_history = 1;
  120.  
  121. /* Non-zero means this shell is restricted. */
  122. int restricted = 0;
  123.  
  124. /* Special debugging helper. */
  125. int debugging_login_shell = 0;
  126.  
  127. /* The environment that the shell passes to other commands. */
  128. char **shell_environment;
  129.  
  130. /* Non-zero when we are executing a top-level command. */
  131. int executing = 0;
  132.  
  133. /* The number of commands executed so far. */
  134. int current_command_number = 1;
  135.  
  136. /* The environment at the top-level REP loop.  We use this in the case of
  137.    error return. */
  138. jmp_buf top_level, catch;
  139.  
  140. #if defined (JOB_CONTROL) || defined (_POSIX_VERSION)
  141. /* The signal masks that this shell runs with. */
  142. sigset_t top_level_mask;
  143. #endif /* JOB_CONTROL */
  144.  
  145. /* Non-zero is the recursion depth for commands. */
  146. int indirection_level = 0;
  147.  
  148. /* The number of times BASH has been executed.  This is set
  149.    by initialize_variables () in variables.c. */
  150. int shell_level = 0;
  151.  
  152. /* The name of this shell, as taken from argv[0]. */
  153. char *shell_name = (char *)NULL;
  154.  
  155. /* time in seconds when the shell was started */
  156. time_t shell_start_time;
  157.  
  158. /* The name of the .(shell)rc file. */
  159. char *bashrc_file = "~/.bashrc";
  160.  
  161. /* Non-zero means to act more like the Bourne shell on startup. */
  162. int act_like_sh = 0;
  163.  
  164. /* Values for the long-winded argument names. */
  165. int debugging = 0;        /* Do debugging things. */
  166. int no_rc = 0;            /* Don't execute ~/.bashrc */
  167. int no_profile = 0;        /* Don't execute .profile */
  168. int do_version = 0;        /* Display interesting version info. */
  169. int quiet = 0;            /* Be quiet when starting up. */
  170. int make_login_shell = 0;    /* Make this shell be a `-bash' shell. */
  171. int no_line_editing = 0;    /* Don't do fancy line editing. */
  172. int no_brace_expansion = 0;    /* Non-zero means no foo{a,b} -> fooa fooa. */
  173.  
  174. /* Some long-winded argument names.  These are obviously new. */
  175. #define Int 1
  176. #define Charp 2
  177. struct {
  178.   char *name;
  179.   int *value;
  180.   int type;
  181. } long_args[] = {
  182.   { "debug", &debugging, Int },
  183.   { "norc", &no_rc, Int },
  184.   { "noprofile", &no_profile, Int },
  185.   { "rcfile", (int *)&bashrc_file, Charp},
  186.   { "version", &do_version, Int},
  187.   { "quiet", &quiet, Int},
  188.   { "login", &make_login_shell, Int},
  189.   { "nolineediting", &no_line_editing, Int},
  190.   { "nobraceexpansion", &no_brace_expansion, Int},
  191.   { (char *)NULL, (int *)0x0, 0 }
  192. };
  193.  
  194. /* The number of lines that Bash has added to this history session. */
  195. int history_lines_this_session = 0;
  196.  
  197. /* The number of lines that Bash has read from the history file. */
  198. int history_lines_in_file = 0;
  199.  
  200. /* These are extern so execute_simple_command can set them, and then
  201.    longjmp back to main to execute a shell script, instead of calling
  202.    main () again and resulting in indefinite, possibly fatal, stack
  203.    growth. */
  204. jmp_buf subshell_top_level;
  205. int subshell_argc;
  206. char **subshell_argv;
  207. char **subshell_envp;
  208.  
  209. main (argc, argv, env)
  210.      int argc;
  211.      char **argv, **env;
  212. {
  213.   extern int last_command_exit_value;
  214.   extern char *base_pathname ();
  215.   register int i;
  216.   int arg_index, locally_skip_execution;
  217.   int top_level_arg_index, read_from_stdin;
  218.   FILE *default_input;
  219.   char *local_pending_command = (char *)NULL;
  220. #if defined (JOB_CONTROL)
  221.   extern int job_control;
  222. #endif /* JOB_CONTROL */
  223.  
  224. #if defined (AUX)
  225. #include <compat.h>
  226.   set42sig ();
  227.   setcompat (getcompat() | COMPAT_BSDGROUPS | COMPAT_BSDSIGNALS |
  228.          COMPAT_BSDTTY | COMPAT_EXEC | COMPAT_SYSCALLS);
  229. #endif /* AUX */
  230.  
  231.   /* There is a bug in the NeXT 2.1 rlogind that causes opens
  232.      of /dev/tty to fail. */
  233. #if defined (NeXT)
  234.   {
  235.     int tty_fd;
  236.  
  237.     tty_fd = open ("/dev/tty", O_RDWR);
  238.  
  239.     if (tty_fd < 0)
  240.       {
  241.     char *tty;
  242.     tty = (char *)ttyname (fileno (stdin));
  243.     tty_fd = open (tty, O_RDWR);
  244.       }
  245.     close (tty_fd);
  246.   }
  247. #endif /* NeXT */
  248.  
  249.   /* Wait forever if we are debugging a login shell. */
  250.   while (debugging_login_shell);
  251.  
  252.   if (setjmp (subshell_top_level))
  253.     {
  254.       argc = subshell_argc;
  255.       argv = subshell_argv;
  256.       env = subshell_envp;
  257.     }
  258.  
  259.   /* Initialize local variables for all `invocations' of main (). */
  260.   arg_index = 1;
  261.   local_pending_command = (char *)NULL;
  262.   locally_skip_execution = 0;
  263.   read_from_stdin = 0;
  264.   default_input = stdin;
  265.  
  266.   /* Fix for the `infinite process creation' bug when running shell scripts
  267.      from startup files on System V. */
  268.   login_shell = make_login_shell = 0;
  269.  
  270.   /* If this shell has already been run, then reinitialize it to a
  271.      vanilla state. */
  272.   if (shell_initialized || shell_name)
  273.     {
  274.       /* Make sure that we do not infinitely recurse as a login shell. */
  275.       if (*shell_name == '-')
  276.     shell_name++;
  277.  
  278.       shell_reinitialize ();
  279.       if (setjmp (top_level))
  280.     exit (2);
  281.     }
  282.  
  283.   /* Here's a hack.  If the name of this shell is "sh", then don't do
  284.      any startup files; just try to be more like /bin/sh. */
  285.   {
  286.     char *tshell_name = base_pathname (argv[0]);
  287.  
  288.     if (*tshell_name == '-')
  289.       tshell_name++;
  290.  
  291.     if (strcmp (tshell_name, "sh") == 0)
  292.       act_like_sh++;
  293.   }
  294.  
  295.   yydebug = 0;
  296.  
  297.   shell_environment = env;
  298.   shell_name = argv[0];
  299.   dollar_vars[0] = savestring (shell_name);
  300.  
  301.   if (*shell_name == '-')
  302.     {
  303.       shell_name++;
  304.       login_shell++;
  305.     }
  306.  
  307. #if defined (JOB_CONTROL)
  308.   if (act_like_sh)
  309.     job_control = 0;
  310. #endif /* JOB_CONTROL */
  311.  
  312.   shell_start_time = NOW;    /* NOW now defined in general.h */
  313.  
  314.   /* A program may start an interactive shell with
  315.         "execl ("/bin/bash", "-", NULL)".
  316.      If so, default the name of this shell to our name. */
  317.   if (!shell_name || !*shell_name || (strcmp (shell_name, "-") == 0))
  318.     shell_name = "bash";
  319.  
  320.   /* Parse argument flags from the input line. */
  321.  
  322.   /* Find full word arguments first. */
  323.   while ((arg_index != argc) && *(argv[arg_index]) == '-')
  324.     {
  325.       for (i = 0; long_args[i].name; i++)
  326.     {
  327.       if (strcmp (&(argv[arg_index][1]), long_args[i].name) == 0)
  328.         {
  329.           if (long_args[i].type == Int)
  330.         *(long_args[i].value) = 1;
  331.           else
  332.         {
  333.           if (!argv[++arg_index])
  334.             {
  335.               report_error ("%s: Flag `%s' expected an argument",
  336.                     shell_name, long_args[i].name);
  337.               exit (1);
  338.             }
  339.           else
  340.             *long_args[i].value = (int)argv[arg_index];
  341.         }
  342.           goto handle_next_arg;
  343.         }
  344.     }
  345.       break;            /* No such argument.  Maybe flag arg. */
  346.     handle_next_arg:
  347.       arg_index++;
  348.     }
  349.  
  350.   /* If user supplied the "-login" flag, then set and invert LOGIN_SHELL. */
  351.   if (make_login_shell)
  352.     login_shell = -++login_shell;
  353.  
  354.   /* All done with full word args; do standard shell arg parsing.*/
  355.   while (arg_index != argc && argv[arg_index] &&
  356.      (*(argv[arg_index]) == '-' || (*argv[arg_index] == '+')))
  357.     {
  358.       /* There are flag arguments, so parse them. */
  359.       int arg_character;
  360.       int on_or_off = (*argv[arg_index]);
  361.       char *o_option;
  362.       int next_arg = arg_index + 1;
  363.  
  364.       i = 1;
  365.  
  366.       /* A single `-' signals the end of options.  From the 4.3 BSD sh.
  367.      An option `--' means the same thing; this is the standard
  368.      getopt () meaning. */
  369.       if (((argv[arg_index][0] == '-') && (argv[arg_index][1] == '\0')) ||
  370.       (strcmp (argv[arg_index], "--") == 0))
  371.     {
  372.       arg_index++;
  373.       goto after_flags;
  374.     }
  375.  
  376.       while (arg_character = (argv[arg_index])[i++])
  377.     {
  378.       switch (arg_character)
  379.         {
  380.         case 'c':
  381.           /* The next arg is a command to execute, and the following args
  382.          are $1 .. $n respectively. */
  383.         local_pending_command = argv[++arg_index];
  384.         if (!local_pending_command)
  385.           {
  386.             report_error ("`%cc' requires an argument", on_or_off);
  387.             exit (1);
  388.           }
  389.         arg_index++;
  390.         goto after_flags;
  391.  
  392.         case 's':
  393.         read_from_stdin = 1;
  394.         break;
  395.  
  396.         case 'o':
  397.         o_option = argv[next_arg++];
  398.         if (!o_option)
  399.           {
  400.             report_error ("`%co' requires an argument", on_or_off);
  401.             exit (1);
  402.           }
  403.  
  404.         if (set_minus_o_option (on_or_off, o_option) != EXECUTION_SUCCESS)
  405.           exit (1);
  406.         break;
  407.  
  408.         default:
  409.           if (change_flag_char (arg_character, on_or_off) == FLAG_ERROR)
  410.         {
  411.           report_error ("%c%c: bad option", on_or_off, arg_character);
  412.           exit (1);
  413.         }
  414.  
  415.         }
  416.     }
  417.       /* Can't do just a simple increment anymore -- what about
  418.      "bash -abouo emacs ignoreeof -hO"? */
  419.       arg_index = next_arg;
  420.     }
  421.  
  422.  after_flags:
  423.  
  424.   /* First, let the outside world know about our interactive status.
  425.      A shell is interactive if the `-i' flag was given, or if all of
  426.      the following conditions are met:
  427.     no -c command
  428.     no arguments remaining or the -s flag given
  429.     standard input is a terminal
  430.     standard output is a terminal
  431.      Refer to Posix.2, the description of the `sh' utility. */
  432.  
  433.   if (forced_interactive ||        /* -i flag */
  434.       (!local_pending_command &&    /* No -c command and ... */
  435.        ((arg_index == argc) ||        /*   no remaining args or... */
  436.     read_from_stdin) &&        /*   -s flag with args, and */
  437.        isatty (fileno (stdin)) &&    /* Input is a terminal and */
  438.        isatty (fileno (stdout))))    /* output is a terminal. */
  439.     {
  440.       interactive_shell = 1;
  441.       interactive = 1;
  442.     }
  443.   else
  444.     {
  445.       history_expansion = 0;
  446.       remember_on_history = 0;
  447.       interactive_shell = 0;
  448.       interactive = 0;
  449. #if defined (JOB_CONTROL)
  450.       job_control = 0;
  451. #endif /* JOB_CONTROL */
  452.     }
  453.  
  454. #define CLOSE_FDS_AT_LOGIN
  455.  
  456. #if defined (CLOSE_FDS_AT_LOGIN)
  457.   /*
  458.    * Some systems have the bad habit of starting login shells with lots of open
  459.    * file descriptors.  For instance, most systems that have picked up the
  460.    * pre-4.0 Sun YP code leave a file descriptor open each time you call one
  461.    * of the getpw* functions, and it's set to be open across execs.  That
  462.    * means one for login, one for xterm, one for shelltool, etc.
  463.    */
  464.   if (login_shell && interactive_shell)
  465.     {
  466.       for (i = 3; i < 20; i++)
  467.     close (i);
  468.     }
  469. #endif /* CLOSE_FDS_AT_LOGIN */
  470.  
  471.   /* From here on in, the shell must be a normal functioning shell.
  472.      Variables from the environment are expected to be set, etc. */
  473.   shell_initialize ();
  474.  
  475.   if (interactive_shell)
  476.     {
  477.       char *term = (char *)getenv ("TERM");
  478.       if (term && (strcmp (term, "emacs") == 0))
  479.     no_line_editing = 1;
  480.     }
  481.  
  482.   top_level_arg_index = arg_index;
  483.  
  484.   if (!quiet && do_version)
  485.     show_shell_version ();
  486.  
  487.   /* Give this shell a place to longjmp to before executing the
  488.      startup files.  This allows users to press C-c to abort the
  489.      lengthy startup. */
  490.   {
  491.     int code;
  492.  
  493.     code = setjmp (top_level);
  494.  
  495.     if (code)
  496.       {
  497.     if (code == EXITPROG)
  498.       goto exit_shell;
  499.     else
  500.       locally_skip_execution++;
  501.       }
  502.   }
  503.  
  504.   arg_index = top_level_arg_index;
  505.  
  506.   /* Execute the start-up scripts. */
  507.  
  508.   if (!interactive_shell)
  509.     {
  510.       makunbound ("PS1", shell_variables);
  511.       makunbound ("PS2", shell_variables);
  512.       interactive = 0;
  513.     }
  514.   else
  515.     {
  516.       change_flag_char ('i', FLAG_ON);
  517.       interactive = 1;
  518.     }
  519.  
  520.   if (!locally_skip_execution)
  521.     {
  522.       if (login_shell)
  523.     {
  524.       /* We don't execute .bashrc for login shells. */
  525.       no_rc++;
  526. #if defined (NOTDEF)
  527.       if (getenv ("POSIXLY_CORRECT"))
  528. #endif /* NOTDEF */
  529.         maybe_execute_file ("/etc/profile");
  530.     }
  531.  
  532.       if (login_shell && !no_profile)
  533.     {
  534.       if (act_like_sh)
  535.         maybe_execute_file ("~/.profile");
  536.       else
  537.         {
  538.           if (maybe_execute_file ("~/.bash_profile") == 0)
  539.         if (maybe_execute_file ("~/.bash_login") == 0)
  540.           maybe_execute_file ("~/.profile");
  541.         }
  542.  
  543.     /* I turn on the restrictions afterwards because it is explictly
  544.        stated in the POSIX spec that PATH cannot be set in a restricted
  545.        shell, except in .profile. */
  546.       if (*++(argv[0]) == 'r')
  547.         {
  548.           set_var_read_only ("PATH");
  549.           set_var_read_only ("SHELL");
  550.           restricted++;
  551.         }
  552.     }
  553.  
  554.       /* Execute ~/.bashrc for most shells.  Never execute it if
  555.      ACT_LIKE_SH is set, or if NO_RC is set.
  556.  
  557.      If the executable file "/usr/gnu/src/bash/foo" contains:
  558.  
  559.        #!/usr/gnu/bin/bash
  560.        echo hello
  561.  
  562.      then:
  563.  
  564.      COMMAND        EXECUTE BASHRC
  565.      --------------------------------
  566.      bash -c foo        NO
  567.      bash foo        NO
  568.      foo            NO
  569.      rsh machine ls        YES (for rsh, which calls `bash -c')
  570.      rsh machine foo    YES (for shell started by rsh) NO (for foo!)
  571.      echo ls | bash        NO
  572.      login            YES
  573.      bash            YES
  574.       */
  575.       if (!act_like_sh && !no_rc &&
  576.       (interactive_shell || (!isatty (fileno (stdin)) &&
  577.                  local_pending_command)))
  578.     maybe_execute_file (bashrc_file);
  579.  
  580.       /* Try a TMB suggestion.  If running a script, then execute the
  581.      file mentioned in the ENV variable. */
  582.       if (!interactive_shell)
  583.     {
  584.       char *env_file = (char *)getenv ("ENV");
  585.       if (env_file && *env_file)
  586.         {
  587.           WORD_LIST *list, *expand_string_unsplit ();
  588.           char *expanded_file_name, *string_list ();
  589.  
  590.           list = expand_string_unsplit (env_file, 1);
  591.           if (list)
  592.         {
  593.           expanded_file_name = string_list (list);
  594.           dispose_words (list);
  595.           if (expanded_file_name && *expanded_file_name)
  596.             maybe_execute_file (expanded_file_name);
  597.           free (expanded_file_name);
  598.         }
  599.         }
  600.     }
  601.  
  602.       if (local_pending_command)
  603.     {
  604.       /* Bind remaining args to $1 ... $n */
  605.       WORD_LIST *args = (WORD_LIST *)NULL;
  606.       while (arg_index != argc)
  607.         args = make_word_list (make_word (argv[arg_index++]), args);
  608.       args = (WORD_LIST *)reverse_list (args);
  609.       remember_args (args, 1);
  610.       dispose_words (args);
  611.  
  612.       with_input_from_string (local_pending_command, "-c");
  613.       goto read_and_execute;
  614.     }
  615.     }
  616.  
  617.   /* Do the things that should be done only for interactive shells. */
  618.   if (interactive_shell)
  619.     {
  620.       /* Set up for checking for presence of mail. */
  621. #if defined (USG)
  622.       /* Under System V, we can only tell if you have mail if the
  623.      modification date has changed.  So remember the current
  624.      modification dates. */
  625.       remember_mail_dates ();
  626. #else
  627.       /* Under 4.x, you have mail if there is something in your inbox.
  628.      I set the remembered mail dates to 1900.  */
  629.       reset_mail_files ();
  630. #endif /* USG */
  631.  
  632.       /* If this was a login shell, then assume that /bin/login has already
  633.      taken care of informing the user that they have new mail.  Otherwise,
  634.      we want to check right away. */
  635.       if (login_shell == 1)
  636.     {
  637. #if !defined (USG)
  638.       remember_mail_dates ();
  639. #endif
  640.     }
  641.  
  642.       reset_mail_timer ();
  643.  
  644.       change_flag_char ('i', FLAG_ON);
  645.  
  646.       /* Initialize the interactive history stuff. */
  647.       if (!shell_initialized)
  648.     load_history ();
  649.  
  650.       /* Initialize terminal state for interactive shells after the
  651.      .bash_profile and .bashrc are interpreted. */
  652.       get_tty_state ();
  653.     }
  654.  
  655.   /* Get possible input filename. */
  656.   if ((arg_index != argc) && !read_from_stdin)
  657.     {
  658.       int fd;
  659.       char *filename;
  660.       extern char *find_path_file ();
  661.  
  662.       free (dollar_vars[0]);
  663.       dollar_vars[0] = savestring (argv[arg_index]);
  664.       filename = savestring (argv[arg_index]);
  665.  
  666.       fd = open (filename, O_RDONLY);
  667.       if ((fd < 0) && (errno == ENOENT))
  668.     {
  669.       char *path_filename;
  670.       /* If it's not in the current directory, try looking through PATH
  671.          for it. */
  672.       path_filename = find_path_file (argv[arg_index]);
  673.       if (path_filename)
  674.         {
  675.           free (filename);
  676.           filename = path_filename;
  677.           fd = open (filename, O_RDONLY);
  678.         }
  679.     }
  680.  
  681.       arg_index++;
  682.       if (fd < 0)
  683.     {
  684.       file_error (filename);
  685.       exit (1);
  686.     }
  687.  
  688.       /* Only do this with file descriptors we can seek on. */
  689.       if (lseek (fd, 0L, 1) != -1)
  690.     {
  691.       unsigned char sample[80];
  692.       int sample_len;
  693.  
  694.       /* Check to see if the `file' in `bash file' is a binary file
  695.          according to the same tests done by execute_simple_command (),
  696.          and report an error and exit if it is. */
  697.       sample_len = read (fd, sample, sizeof (sample));
  698.       if (sample_len > 0)
  699.         if (check_binary_file (sample, sample_len))
  700.           {
  701.         report_error ("%s: cannot execute binary file", filename);
  702.         exit (EX_BINARY_FILE);
  703.           }
  704.       /* Now rewind the file back to the beginning. */
  705.       lseek (fd, 0L, 0);
  706.     }
  707.  
  708.       default_input = fdopen (fd, "r");
  709.  
  710.       if (!default_input)
  711.     {
  712.       file_error (filename);
  713.       exit (127);
  714.     }
  715.  
  716.       SET_CLOSE_ON_EXEC (fd);
  717.       if (fileno (default_input) != fd)
  718.     SET_CLOSE_ON_EXEC (fileno (default_input));
  719.  
  720.       if (!interactive_shell || (!isatty (fd)))
  721.     {
  722.       history_expansion = 0;
  723.       remember_on_history = 0;
  724.       interactive = interactive_shell = 0;
  725. #if defined (JOB_CONTROL)
  726.       set_job_control (0);
  727. #endif /* JOB_CONTROL */
  728.     }
  729.       else
  730.     {
  731.       /* I don't believe that this code is ever executed, even in
  732.          the presence of /dev/fd. */
  733.       dup2 (fd, 0);
  734.       close (fd);
  735.       fclose (default_input);
  736.     }
  737.     }
  738.  
  739.   /* Bind remaining args to $1 ... $n */
  740.   {
  741.     WORD_LIST *args = (WORD_LIST *)NULL;
  742.     while (arg_index != argc)
  743.       args = make_word_list (make_word (argv[arg_index++]), args);
  744.     args = (WORD_LIST *)reverse_list (args);
  745.     remember_args (args, 1);
  746.     dispose_words (args);
  747.   }
  748.  
  749.   unset_nodelay_mode (fileno (stdin));
  750.  
  751.   /* with_input_from_stdin really means `with_input_from_readline' */
  752.   if (interactive && !no_line_editing)
  753.     with_input_from_stdin ();
  754.   else
  755.     with_input_from_stream (default_input, dollar_vars[0]);
  756.  
  757.  read_and_execute:
  758.  
  759.   shell_initialized = 1;
  760.  
  761.   /* Read commands until exit condition. */
  762.   reader_loop ();
  763.  
  764.   exit_shell:
  765.   /* Do trap[0] if defined. */
  766.   run_exit_trap ();
  767.  
  768.   maybe_save_shell_history ();
  769.  
  770. #if defined (JOB_CONTROL)
  771.   /* If this shell is interactive, terminate all stopped jobs and
  772.      restore the original terminal process group. */
  773.   if (interactive_shell)
  774.     {
  775.       terminate_stopped_jobs ();
  776.  
  777.       if (original_pgrp >= 0)
  778.     give_terminal_to (original_pgrp);
  779.     }
  780. #endif /* JOB_CONTROL */
  781.  
  782.   /* Always return the exit status of the last command to our parent. */
  783.   exit (last_command_exit_value);
  784. }
  785.  
  786. /* If this is an interactive shell, then append the lines executed
  787.    this session to the history file. */
  788. int
  789. maybe_save_shell_history ()
  790. {
  791.   int result = 0;
  792.  
  793.   if (interactive && history_lines_this_session)
  794.     {
  795.       void using_history ();
  796.       char *hf = get_string_value ("HISTFILE");
  797.  
  798.       if (hf && *hf)
  799.     {
  800.       struct stat buf;
  801.  
  802.       /* If the file doesn't exist, then create it. */
  803.       if (stat (hf, &buf) == -1)
  804.         {
  805.           int file = open (hf, O_CREAT | O_TRUNC | O_WRONLY, 0666);
  806.           if (file != -1)
  807.         close (file);
  808.         }
  809.  
  810.       /* Now actually append the lines if the history hasn't been
  811.          stifled. */
  812.       using_history ();
  813.       if (history_lines_this_session <= where_history ())
  814.         {
  815.           result = append_history (history_lines_this_session, hf);
  816.           history_lines_in_file += history_lines_this_session;
  817.           history_lines_this_session = 0;
  818.         }
  819.     }
  820.     }
  821.   return (result);
  822. }
  823.  
  824. /* Try to execute the contents of FNAME.  If FNAME doesn't exist,
  825.    that is not an error, but other kinds of errors are.  Returns
  826.    -1 in the case of an error, 0 in the case that the file was not
  827.    found, and 1 if the file was found and executed. */
  828. maybe_execute_file (fname)
  829.      char *fname;
  830. {
  831.   extern char *tilde_expand ();
  832.   extern int return_catch_flag;
  833.   extern jmp_buf return_catch;
  834.   jmp_buf old_return_catch;
  835.   int return_val, fd, tresult;
  836.   char *filename, *string;
  837.   struct stat file_info;
  838.  
  839.   filename = tilde_expand (fname);
  840.   fd = open (filename, O_RDONLY);
  841.  
  842.   if (fd < 0)
  843.     {
  844. file_error_and_exit:
  845.       if (errno != ENOENT)
  846.     file_error (filename);
  847.       free (filename);
  848.       return ((errno == ENOENT) ? 0 : -1);
  849.     }
  850.  
  851.   if (fstat (fd, &file_info) == -1)
  852.     goto file_error_and_exit;
  853.  
  854.   string = (char *)xmalloc (1 + file_info.st_size);
  855.   tresult = read (fd, string, file_info.st_size);
  856.  
  857.   {
  858.     int tt = errno;
  859.     close (fd);
  860.     errno = tt;
  861.   }
  862.  
  863.   if (tresult != file_info.st_size)
  864.     {
  865.       free (string);
  866.       goto file_error_and_exit;
  867.     }
  868.   string[file_info.st_size] = '\0';
  869.  
  870.   return_catch_flag++;
  871.   bcopy ((char *)return_catch, (char *)old_return_catch, sizeof (jmp_buf));
  872.  
  873.   return_val = setjmp (return_catch);
  874.  
  875.   /* If `return' was seen outside of a function, but in the script, then
  876.      force parse_and_execute () to clean up. */
  877.   if (return_val)
  878.     parse_and_execute_cleanup ();
  879.   else
  880.     tresult = parse_and_execute (string, filename);
  881.  
  882.   return_catch_flag--;
  883.   bcopy ((char *)old_return_catch, (char *)return_catch, sizeof (jmp_buf));
  884.  
  885.   free (filename);
  886.  
  887.   return (1);
  888. }
  889.  
  890. reader_loop ()
  891. {
  892.   extern int indirection_level;
  893.   int our_indirection_level;
  894.   COMMAND *current_command = (COMMAND *)NULL;
  895.  
  896.   our_indirection_level = ++indirection_level;
  897.  
  898.   while (!EOF_Reached)
  899.     {
  900.       extern char *trap_list[];
  901.       sighandler sigint_sighandler ();
  902.       int code;
  903.  
  904.       code = setjmp (top_level);
  905.  
  906.       if (interactive_shell)
  907.     {
  908. #if defined (_POSIX_VERSION)
  909.       /* If we are running on a posix-compliant system, then do
  910.          things the Posix way. */
  911.       struct sigaction act;
  912.  
  913.       act.sa_handler = sigint_sighandler;
  914.       act.sa_flags = 0;
  915.       sigemptyset (&act.sa_mask);
  916.       sigaction (SIGINT, &act, (struct sigaction *)NULL);
  917. #else /* !_POSIX_VERSION */
  918.       signal (SIGINT, sigint_sighandler);
  919. #endif /* !_POSIX_VERSION */
  920.     }
  921.  
  922.       if (code != NOT_JUMPED)
  923.     {
  924.       indirection_level = our_indirection_level;
  925.  
  926.       switch (code)
  927.         {
  928.           /* Some kind of throw to top_level has occured. */
  929.         case FORCE_EOF:
  930.         case EXITPROG:
  931.           current_command = (COMMAND *)NULL;
  932.           EOF_Reached = EOF;
  933.           goto exec_done;
  934.  
  935.         case DISCARD:
  936.           /* Obstack free command elements, etc. */
  937.           break;
  938.  
  939.         default:
  940.           programming_error ("Bad jump %d", code);
  941.         }
  942.     }
  943.  
  944.       executing = 0;
  945.       dispose_used_env_vars ();
  946.  
  947. #if (defined (Ultrix) && defined (mips)) || !defined (HAVE_ALLOCA)
  948.       /* Attempt to reclaim memory allocated with alloca (). */
  949.       (void) alloca (0);
  950. #endif
  951.  
  952.       if (read_command () == 0)
  953.     {
  954.       if (global_command)
  955.         {
  956.           current_command = global_command;
  957.  
  958.           current_command_number++;
  959.  
  960.           /* POSIX spec: "-n: The shell reads commands but does
  961.          not execute them; this can be used to check for shell
  962.          script syntax errors.  The shell ignores the -n option
  963.          for interactive shells. " */
  964.           if (interactive_shell || !read_but_dont_execute)
  965.         {
  966.           executing = 1;
  967.           execute_command (current_command);
  968.         }
  969.  
  970.         exec_done:
  971.           if (current_command)
  972.         dispose_command (current_command);
  973.           QUIT;
  974.         }
  975.     }
  976.       else
  977.     {
  978.       /* Parse error, maybe discard rest of stream if not interactive. */
  979.       if (!interactive)
  980.         EOF_Reached = EOF;
  981.     }
  982.       if (just_one_command)
  983.     EOF_Reached = EOF;
  984.     }
  985.   indirection_level--;
  986. }
  987.  
  988. /* Return a string denoting what our indirection level is. */
  989. static char indirection_string[100];
  990.  
  991. char *
  992. indirection_level_string ()
  993. {
  994.   register int i, j;
  995.   char *get_string_value (), *ps4 = get_string_value ("PS4");
  996.   extern char *decode_prompt_string ();
  997.  
  998.   if (!ps4)
  999.     ps4 = savestring ("+ ");
  1000.   else
  1001.     ps4 = decode_prompt_string (ps4);
  1002.  
  1003.   for (i = 0; i < indirection_level && i < 99; i++)
  1004.     indirection_string[i] = *ps4;
  1005.  
  1006.   for (j = 1; ps4[j] && i < 99; i++, j++)
  1007.     indirection_string[i] = ps4[j];
  1008.  
  1009.   indirection_string[i] = '\0';
  1010.   free (ps4);
  1011.   return (indirection_string);
  1012. }
  1013.  
  1014. static sighandler 
  1015. alrm_catcher(i)
  1016.      int i;
  1017. {
  1018.   printf ("%ctimed out waiting for input: auto-logout\n", '\07');
  1019.   longjmp (top_level, EXITPROG);
  1020. #if !defined (VOID_SIGHANDLER)
  1021.   return (0);
  1022. #endif /* !VOID_SIGHANDLER */
  1023. }
  1024.  
  1025. parse_command ()
  1026. {
  1027.   extern int need_here_doc, current_command_line_count;
  1028.   extern REDIRECT *redirection_needing_here_doc;
  1029.   int r;
  1030.  
  1031.   need_here_doc = 0;
  1032.   redirection_needing_here_doc = (REDIRECT *)NULL;
  1033.  
  1034.   run_pending_traps ();
  1035.  
  1036.   current_command_line_count = 0;
  1037.   r = yyparse ();
  1038.  
  1039.   if (need_here_doc)
  1040.     make_here_document (redirection_needing_here_doc);
  1041.   need_here_doc = 0;
  1042.  
  1043.   return (r);
  1044. }
  1045.  
  1046. read_command ()
  1047. {
  1048.   extern char *ps1_prompt, **prompt_string_pointer;
  1049.   extern int current_command_line_count;
  1050.   SHELL_VAR *tmout_var = (SHELL_VAR *)NULL;
  1051.   int tmout_len = 0, result;
  1052.   SigHandler *old_alrm = (SigHandler *)NULL;
  1053.  
  1054.   prompt_string_pointer = &ps1_prompt;
  1055.   global_command = (COMMAND *)NULL;
  1056.  
  1057.   /* Only do timeouts if interactive. */
  1058.   if (interactive)
  1059.     {
  1060.       tmout_var = find_variable ("TMOUT");
  1061.  
  1062.       if (tmout_var && tmout_var->value)
  1063.     {
  1064.       tmout_len = atoi (tmout_var->value);
  1065.       if (tmout_len > 0)
  1066.         {
  1067.           old_alrm = signal (SIGALRM, alrm_catcher);
  1068.           alarm (tmout_len);
  1069.         }
  1070.     }
  1071.     }
  1072.  
  1073.   QUIT;
  1074.  
  1075.   current_command_line_count = 0;
  1076.   result = parse_command ();
  1077.  
  1078.   if (interactive && tmout_var && (tmout_len > 0))
  1079.     {
  1080.       alarm(0);
  1081.       signal (SIGALRM, old_alrm);
  1082.     }
  1083.   return (result);
  1084. }
  1085.  
  1086. /* Do whatever is necessary to initialize the shell.
  1087.    Put new initializations in here. */
  1088. shell_initialize ()
  1089. {
  1090.   /* Line buffer output for stderr.
  1091.      If your machine doesn't have either of setlinebuf or setvbuf,
  1092.      you can just comment out the buffering commands, and the shell
  1093.      will still work.  It will take more cycles, though. */
  1094. #if defined (HAVE_SETLINEBUF)
  1095.   setlinebuf (stderr);
  1096.   setlinebuf (stdout);
  1097. #else
  1098. #  if defined (_IOLBF)
  1099. #    if defined (REVERSED_SETVBUF_ARGS)
  1100.   setvbuf (stderr, _IOLBF, (char *)NULL, BUFSIZ);
  1101.   setvbuf (stdout, _IOLBF, (char *)NULL, BUFSIZ);
  1102. #    else
  1103.   setvbuf (stderr, (char *)NULL, _IOLBF, BUFSIZ);
  1104.   setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
  1105. #    endif /* !REVERSED_SETVBUF_ARGS */
  1106. #  endif /* _IOLBF */
  1107. #endif /* HAVE_SETLINEBUF */
  1108.  
  1109.   /* Sort the array of shell builtins so that the binary search in
  1110.      find_shell_builtin () works correctly. */
  1111.   initialize_shell_builtins ();
  1112.  
  1113.   /* Initialize the trap signal handlers before installing our own
  1114.      signal handlers.  traps.c:restore_default_signal () is responsible
  1115.      for restoring the original default signal handler.  That function
  1116.      is called from jobs.c when we make a new child. */
  1117.   initialize_traps ();
  1118.   initialize_signals ();
  1119.  
  1120.   /* Initialize current_user_name and current_host_name. */
  1121.   {
  1122.     struct passwd *entry = getpwuid (getuid ());
  1123.     char hostname[256];
  1124.  
  1125.     if (gethostname (hostname, 255) < 0)
  1126.       current_host_name = "??host??";
  1127.     else
  1128.       current_host_name = savestring (hostname);
  1129.  
  1130.     if (entry)
  1131.       current_user_name = savestring (entry->pw_name);
  1132.     else
  1133.       current_user_name = savestring ("I have no name!");
  1134.     endpwent ();
  1135.   }
  1136.  
  1137.   /* Initialize our interface to the tilde expander. */
  1138.   tilde_initialize ();
  1139.  
  1140.   /* Initialize internal and environment variables. */
  1141.   initialize_shell_variables (shell_environment);
  1142.  
  1143.   /* Initialize filename hash tables. */
  1144.   initialize_filename_hashing ();
  1145.  
  1146.   /* Initialize the data structures for storing and running jobs. */
  1147.   initialize_jobs ();
  1148.  
  1149.   /* Initialize input streams to null. */
  1150.   initialize_bash_input ();
  1151. }
  1152.  
  1153. /* Function called by main () when it appears that the shell has already
  1154.    had some initialization preformed.  This is supposed to reset the world
  1155.    back to a pristine state, as if we had been exec'ed. */
  1156. shell_reinitialize ()
  1157. {
  1158.   extern int line_number, last_command_exit_value;
  1159.  
  1160.   /* The default shell prompts. */
  1161.   primary_prompt = PPROMPT;
  1162.   secondary_prompt = SPROMPT;
  1163.  
  1164.   /* Things that get 1. */
  1165.   current_command_number = 1;
  1166.  
  1167.   /* We have decided that the ~/.bashrc file should not be executed
  1168.      for the invocation of each shell script.  Perhaps some other file
  1169.      should.  */
  1170.   act_like_sh = 1;
  1171.  
  1172.   /* Things that get 0. */
  1173.   login_shell = make_login_shell = interactive = restricted = executing = 0;
  1174.   debugging = no_rc = no_profile = do_version = line_number = 0;
  1175.   last_command_exit_value = remember_on_history = 0;
  1176.   forced_interactive = interactive_shell = 0;
  1177.  
  1178.   /* Ensure that the default startup file is used.  (Except that we don't
  1179.      execute this file for reinitialized shells). */
  1180.   bashrc_file = "~/.bashrc";
  1181.  
  1182.   /* Delete all variables and functions.  They will be reinitialized when
  1183.      the environment is parsed. */
  1184.  
  1185.   delete_all_variables (shell_variables);
  1186.   delete_all_variables (shell_functions);
  1187.  
  1188.   /* Pretend the PATH variable has changed. */
  1189.   sv_path ("PATH");
  1190. }
  1191.  
  1192. initialize_signals ()
  1193. {
  1194.   initialize_terminating_signals ();
  1195.   initialize_job_signals ();
  1196. #if defined (INITIALIZE_SIGLIST)
  1197.   initialize_siglist ();
  1198. #endif
  1199. }
  1200.  
  1201. /* The list of signals that would terminate the shell if not caught.
  1202.    We catch them, but just so that we can write the history file,
  1203.    and so forth. */
  1204. int terminating_signals[] = {
  1205. #ifdef SIGHUP
  1206.   SIGHUP,
  1207. #endif
  1208.  
  1209. #ifdef SIGINT
  1210.   SIGINT,
  1211. #endif
  1212.  
  1213. #ifdef SIGQUIT
  1214.   SIGQUIT,
  1215. #endif
  1216.  
  1217. #ifdef SIGILL
  1218.   SIGILL,
  1219. #endif
  1220.  
  1221. #ifdef SIGTRAP
  1222.   SIGTRAP,
  1223. #endif
  1224.  
  1225. #ifdef SIGIOT
  1226.   SIGIOT,
  1227. #endif
  1228.  
  1229. #ifdef SIGDANGER
  1230.   SIGDANGER,
  1231. #endif
  1232.  
  1233. #ifdef SIGEMT
  1234.   SIGEMT,
  1235. #endif
  1236.  
  1237. #ifdef SIGFPE
  1238.   SIGFPE,
  1239. #endif
  1240.  
  1241. #ifdef SIGKILL
  1242.   SIGKILL,
  1243. #endif
  1244.  
  1245. #ifdef SIGBUS
  1246.   SIGBUS,
  1247. #endif
  1248.  
  1249. #ifdef SIGSEGV
  1250.   SIGSEGV,
  1251. #endif
  1252.  
  1253. #ifdef SIGSYS
  1254.   SIGSYS,
  1255. #endif
  1256.  
  1257. #ifdef SIGPIPE
  1258.   SIGPIPE,
  1259. #endif
  1260.  
  1261. #ifdef SIGALRM
  1262.   SIGALRM,
  1263. #endif
  1264.  
  1265. #ifdef SIGTERM
  1266.   SIGTERM,
  1267. #endif
  1268.  
  1269. #ifdef SIGXCPU
  1270.   SIGXCPU,
  1271. #endif
  1272.  
  1273. #ifdef SIGXFSZ
  1274.   SIGXFSZ,
  1275. #endif
  1276.  
  1277. #ifdef SIGVTALRM
  1278.   SIGVTALRM,
  1279. #endif
  1280.  
  1281. #ifdef SIGPROF
  1282.   SIGPROF,
  1283. #endif
  1284.  
  1285. #ifdef SIGLOST
  1286.   SIGLOST,
  1287. #endif
  1288.  
  1289. #ifdef SIGUSR1
  1290.   SIGUSR1, SIGUSR2
  1291. #endif
  1292.     };
  1293.  
  1294. #define TERMSIGS_LENGTH (sizeof (terminating_signals) / sizeof (int))
  1295.  
  1296. /* This function belongs here? */
  1297. sighandler
  1298. termination_unwind_protect (sig)
  1299.      int sig;
  1300. {
  1301.   if (sig == SIGINT)
  1302.     run_interrupt_trap ();
  1303.  
  1304.   maybe_save_shell_history ();
  1305.  
  1306. #if defined (JOB_CONTROL)
  1307.   if (sig == SIGHUP)
  1308.     {
  1309.       extern void hangup_all_jobs ();
  1310.  
  1311.       hangup_all_jobs ();
  1312.     }
  1313. #endif /* JOB_CONTROL */
  1314.  
  1315.   run_exit_trap ();
  1316.   signal (sig, SIG_DFL);
  1317.   kill (getpid (), sig);
  1318.  
  1319. #if !defined (VOID_SIGHANDLER)
  1320.   return (0);
  1321. #endif /* VOID_SIGHANDLER */
  1322. }
  1323.  
  1324. /* Initialize signals that will terminate the shell to do some
  1325.    unwind protection. */
  1326. initialize_terminating_signals ()
  1327. {
  1328.   register int i;
  1329.  
  1330. #if defined (_POSIX_VERSION)
  1331.   /* If we're running on a Posix-compliant system, do things the Posix way. */
  1332.  
  1333.   struct sigaction act;
  1334.  
  1335.   act.sa_handler = termination_unwind_protect;
  1336.   act.sa_flags = 0;
  1337.   sigemptyset (&act.sa_mask);
  1338.   for (i = 0; i < TERMSIGS_LENGTH; i++)
  1339.     sigaddset (&act.sa_mask, terminating_signals[i]);
  1340.   for (i = 0; i < TERMSIGS_LENGTH; i++)
  1341.     sigaction (terminating_signals[i], &act, (struct sigaction *)NULL);
  1342.  
  1343.   /* For interactive login shells, use an empty signal mask.  Other
  1344.      shells use what they have been given. */
  1345.   sigemptyset (&top_level_mask);
  1346.  
  1347.   if (login_shell)
  1348.     {
  1349.       sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
  1350.     }
  1351.   else
  1352.     {
  1353.       sigprocmask (SIG_BLOCK, (sigset_t *)NULL, &top_level_mask);
  1354.       sigdelset (&top_level_mask, SIGCHLD);
  1355.     }
  1356. #else /* !_POSIX_VERSION */
  1357.  
  1358.   for (i = 0; i < TERMSIGS_LENGTH; i++)
  1359.     signal (terminating_signals[i], termination_unwind_protect);
  1360.  
  1361. #endif /* !_POSIX_VERSION */
  1362.  
  1363.   /* And, some signals that are specifically ignored by the shell. */
  1364.   signal (SIGQUIT, SIG_IGN);
  1365.  
  1366.   if (interactive)
  1367.     signal (SIGTERM, SIG_IGN);
  1368. }
  1369.  
  1370. /* What to do when we've been interrupted, and it is safe to handle it. */
  1371. void
  1372. throw_to_top_level ()
  1373. {
  1374.   extern int last_command_exit_value, loop_level, continuing, breaking;
  1375.   extern int return_catch_flag;
  1376.   extern int parse_and_execute_level;
  1377.   int print_newline = 0;
  1378.  
  1379.   if (interrupt_state)
  1380.     {
  1381.       print_newline = 1;
  1382.       interrupt_state--;
  1383.     }
  1384.  
  1385.   if (interrupt_state)
  1386.     return;
  1387.  
  1388.   /* Run any traps set on SIGINT. */
  1389.   run_interrupt_trap ();
  1390.  
  1391.   /* Cleanup string parser environment. */
  1392.   while (parse_and_execute_level)
  1393.     parse_and_execute_cleanup ();
  1394.  
  1395. #if defined (JOB_CONTROL)
  1396.   give_terminal_to (shell_pgrp);
  1397.   sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
  1398. #endif /* JOB_CONTROL */
  1399.  
  1400.   reset_parser ();
  1401.  
  1402. #if defined (READLINE)
  1403.   if (interactive)
  1404.     bashline_reinitialize ();
  1405. #endif /* READLINE */
  1406.  
  1407.   run_unwind_protects ();
  1408.   loop_level = continuing = breaking = 0;
  1409.   return_catch_flag = 0;
  1410.  
  1411.   if (interactive && print_newline)
  1412.     {
  1413.       fflush (stdout);
  1414.       fprintf (stderr, "\n");
  1415.     }
  1416.  
  1417.   last_command_exit_value |= 128;
  1418.  
  1419.   if (interactive)
  1420.     longjmp (top_level, DISCARD);
  1421.   else
  1422.     longjmp (top_level, EXITPROG);
  1423. }
  1424.  
  1425. /* When non-zero, we throw_to_top_level (). */
  1426. int interrupt_immediately = 0;
  1427.  
  1428. /* What we really do when SIGINT occurs. */
  1429. sighandler
  1430. sigint_sighandler (sig)
  1431.      int sig;
  1432. {
  1433. #if defined (USG) && !defined (_POSIX_VERSION)
  1434.   signal (sig, sigint_sighandler);
  1435. #endif
  1436.  
  1437.   /* interrupt_state needs to be set for the stack of interrupts to work
  1438.      right.  Should it be set unconditionally? */
  1439.   if (!interrupt_state)
  1440.     interrupt_state++;
  1441.   if (interrupt_immediately)
  1442.     {
  1443.       interrupt_immediately = 0;
  1444.       throw_to_top_level ();
  1445.     }
  1446. #if !defined (VOID_SIGHANDLER)
  1447.   return (0);
  1448. #endif /* VOID_SIGHANDLER */
  1449. }
  1450.  
  1451. /* Load the history list from the history file. */
  1452. load_history ()
  1453. {
  1454.   char *hf;
  1455.  
  1456.   /* Truncate history file for interactive shells which desire it.
  1457.      Note that the history file is automatically truncated to the
  1458.      size of HISTSIZE if the user does not explicitly set the size
  1459.      differently. */
  1460.   set_if_not ("HISTFILESIZE", get_string_value ("HISTSIZE"));
  1461.   stupidly_hack_special_variables ("HISTFILESIZE");
  1462.  
  1463.   /* Read the history in HISTFILE into the history list. */
  1464.   hf = get_string_value ("HISTFILE");
  1465.  
  1466.   if (hf && *hf)
  1467.     {
  1468.       struct stat buf;
  1469.  
  1470.       if (stat (hf, &buf) == 0)
  1471.     {
  1472.       read_history (hf);
  1473.       using_history ();
  1474.       history_lines_in_file = where_history ();
  1475.     }
  1476.     }
  1477. }
  1478.  
  1479. /* Write the existing history out to the history file. */
  1480. save_history ()
  1481. {
  1482.   char *hf = get_string_value ("HISTFILE");
  1483.  
  1484.   if (hf && *hf)
  1485.     {
  1486.       struct stat buf;
  1487.  
  1488.       if (stat (hf, &buf) == 0)
  1489.     {
  1490.       /* Append only the lines that occurred this session to
  1491.          the history file. */
  1492.       using_history ();
  1493.  
  1494.       if (history_lines_this_session < where_history ())
  1495.         append_history (history_lines_this_session, hf);
  1496.       else
  1497.         write_history (hf);
  1498.     }
  1499.     }
  1500.  
  1501. }
  1502.  
  1503. #if defined (MAKE_BUG_REPORTS)
  1504. /* Make a bug report, even to the extent of mailing it.  Hope that it
  1505.    gets where it is supposed to go.  If not, maybe the user will send
  1506.    it back to me. */
  1507. #include <readline/history.h>
  1508. /* Number of commands to place in the bug report. */
  1509. #define LAST_INTERESTING_HISTORY_COUNT 6
  1510.  
  1511. #if defined (HAVE_VFPRINTF)
  1512. make_bug_report (va_alist)
  1513.      va_dcl
  1514. #else
  1515. make_bug_report (reason, arg1, arg2)
  1516.      char *reason;
  1517. #endif /* HAVE_VFPRINTF */
  1518. {
  1519.   extern char *current_host_name, *current_user_name, *the_current_maintainer;
  1520.   extern int interactive, login_shell;
  1521.   register int len = where_history ();
  1522.   register int i = len - LAST_INTERESTING_HISTORY_COUNT;
  1523.   FILE *stream, *popen ();
  1524.   HIST_ENTRY **list = history_list ();
  1525.  
  1526. #if defined (HAVE_VFPRINTF)
  1527.   char *reason;
  1528.   va_list args;
  1529. #endif /* HAVE_VFPRINTF */
  1530.  
  1531.   stream = popen ("/bin/rmail bash-maintainers@ai.mit.edu", "w");
  1532.  
  1533.   save_history ();
  1534.   if (i < 0) i = 0;
  1535.  
  1536.   if (stream)
  1537.     {
  1538.       fprintf (stream, "To: bash-maintainers@ai.mit.edu\n");
  1539.       fprintf (stream, "Subject: Bash-%s.%d bug-report: ",
  1540.            dist_version, build_version);
  1541.  
  1542. #if defined (HAVE_VFPRINTF)
  1543.       va_start (args);
  1544.       reason = va_arg (args, char *);
  1545.       vfprintf (stream, reason, args);
  1546.       va_end (args);
  1547. #else
  1548.       fprintf (stream, reason, arg1, arg2);
  1549. #endif /* HAVE_VFPRINTF */
  1550.  
  1551.       fprintf (stream, "\n");
  1552.  
  1553.       /* Write the history into the mail file.  Maybe we can recreate
  1554.      the bug? */
  1555.       fprintf (stream,
  1556.            "This is a Bash bug report.  Bash maintainers should be getting this report.\n\
  1557. If this mail has bounced, for right now please send it to:\n\
  1558. \n\
  1559.     %s\n\
  1560. \n\
  1561. since he is the current maintainer of this version of the shell.\n\
  1562. \n\
  1563. This is %s (invoked as `%s'), version %s.%d, on host %s, used by %s.\n\
  1564. This shell is %sinteractive, and it is %sa login shell.\n\
  1565. \n\
  1566. The host is a %s running %s.\n\
  1567. \n\
  1568. The current environment is:\n",
  1569.            the_current_maintainer,
  1570.            get_string_value ("BASH"), full_pathname (dollar_vars[0]),
  1571.            dist_version, build_version, current_host_name,
  1572.            current_user_name, interactive ? "" : "not ",
  1573.            login_shell ? "" : "not ", SYSTEM_NAME, OS_NAME);
  1574.  
  1575.       {
  1576.     SHELL_VAR **vlist, *var;
  1577.     register int i;
  1578.  
  1579.     vlist = all_shell_variables ();
  1580.  
  1581.     for (i = 0; vlist && var = vlist[i]; i++)
  1582.       {
  1583.         if (!invisible_p (var) && exported_p (var))
  1584.           {
  1585.         fprintf (stream, "%s=%s", var->name, value_cell (var));
  1586.         fprintf (stream, "\n");
  1587.           }
  1588.       }
  1589.       }
  1590.  
  1591.       fprintf (stream, "\nAnd here are the last %d commands.\n\n",
  1592.            LAST_INTERESTING_HISTORY_COUNT);
  1593.  
  1594.       for (; i < len; i++)
  1595.     fprintf (stream, "%s\n", list[i]->line);
  1596.  
  1597.       pclose (stream);
  1598.     }
  1599.   else
  1600.     {
  1601.       fprintf (stderr, "Can't mail bug report!\n");
  1602.     }
  1603. }
  1604. #endif /* MAKE_BUG_REPORTS */
  1605.  
  1606. /* Give version information about this shell. */
  1607. show_shell_version ()
  1608. {
  1609.   extern char *base_pathname ();
  1610.   extern char *shell_name;
  1611.   extern int version;
  1612.  
  1613.   printf ("GNU %s, version %s.%d\n", base_pathname (shell_name),
  1614.       dist_version, build_version);
  1615. }
  1616.